package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.ApplyCustomsBiz;
import com.egao.orderB.biz.ApplyCustomsFeeBiz;
import com.egao.orderB.biz.BillBiz;
import com.egao.orderB.entity.*;
import com.egao.orderB.service.ApplyCustomsService;
import com.egao.orderB.service.BillOceanApplyService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.egao.orderB.mapper.ApplyCustomsMapper;
//import javax.transaction.Transactional;

/**
 * 报关服务实现类
 * @author : Doman
 * @date : 2021-07-21 14:58:25
 * @version ：1.0
 */
@Service
public class ApplyCustomsServiceImpl /*extends ServiceImpl<ApplyCustomsMapper, ApplyCustoms> */implements ApplyCustomsService {

    @Autowired
    private ApplyCustomsBiz applyCustomsBiz;
    @Autowired
    private BillOceanApplyService billOceanApplyService;
    @Autowired
    private ApplyCustomsFeeBiz applyCustomsFeeBiz;
    @Autowired
    private BillBiz billBiz;

    /**
    @Override
    public PageResult<ApplyCustoms> listPage(PageParam<ApplyCustoms> page) {
        List<ApplyCustoms> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<ApplyCustoms> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, ApplyCustoms applyCustoms, User loginUser)  throws Exception{
        return applyCustomsBiz.list(page,applyCustoms,loginUser);
    }
    @Override
    public JsonResult listDel(PageResult2<T> page, ApplyCustoms applyCustoms, User loginUser)  throws Exception{
        return applyCustomsBiz.listDel(page,applyCustoms,loginUser);
    }

    @Override
    public JsonResult list(ApplyCustoms applyCustoms, User loginUser) {
        return JsonResult.ok().setData(applyCustomsBiz.list(applyCustoms,loginUser));
    }

    @Override
    public JsonResult listOpen(ApplyCustoms applyCustoms, User loginUser) {
        ApplyCustoms b = applyCustomsBiz.getById(applyCustoms);
        ApplyCustomsFee bf = new ApplyCustomsFee();
        bf.setApplyId(b.getId());
        b.setApplyCustomsFees(applyCustomsFeeBiz.list(bf, loginUser));
        return JsonResult.ok().setData(b);
    }
    @Override
    public List<ApplyCustoms> exportList(ApplyCustoms applyCustoms, User loginUser) {
        applyCustoms.setSort("asc");
        List<ApplyCustoms> applyCustomsList = applyCustomsBiz.list(applyCustoms, loginUser);
        if(applyCustomsList != null && applyCustomsList.size() > 0) {
            for (int i = 0; i < applyCustomsList.size(); i++) {
                ApplyCustoms b = applyCustomsList.get(i);
                ApplyCustomsFee applyCustomsFee = new ApplyCustomsFee();
                applyCustomsFee.setApplyId(b.getId());
//                applyCustomsFee.setInvoicename(b.getCustomer());
                b.setApplyCustomsFees(applyCustomsFeeBiz.list(applyCustomsFee, new User(1)));
            }
        }
        return applyCustomsList;
    }

    @Override
    public JsonResult getById(ApplyCustoms applyCustoms) {
        return JsonResult.ok().setData(applyCustomsBiz.getById(applyCustoms));
    }

    @Transactional
    @Override
    public JsonResult save(ApplyCustoms applyCustoms, User loginUser) {
        if (Utils.isEmpty(applyCustoms.getCustomsDate())) {
            applyCustoms.setCustomsDate(DateUtil.getCurrentDate());
            if (Utils.isNotEmpty(applyCustoms.getBillId())) {
                Bill bill = new Bill();
                bill.setId(applyCustoms.getBillId()+"");
                bill= billBiz.getById(bill);
                applyCustoms.setCustomsDate(bill.getPackDate());
            }
        }
        int key  = applyCustomsBiz.save(applyCustoms, loginUser);
        if(Utils.isNotEmpty(applyCustoms.getBillId())) {
            BillOceanApply billOceanApply = new BillOceanApply();
            billOceanApply.setKeywords("bill");
            billOceanApply.setBillId(Integer.valueOf(applyCustoms.getBillId()));
            billOceanApply.setApplyId(key);
            billOceanApplyService.setTriAngle(billOceanApply, loginUser);
        }
        return JsonResult.ok("添加成功");
    }

    @Transactional
    @Override
    public JsonResult release(ApplyCustoms applyCustoms, User loginUser) {
        if (applyCustomsBiz.release(applyCustoms, loginUser)) {
            return JsonResult.ok("放行成功");
        }
        return JsonResult.error("放行失败");
    }

    @Transactional
    @Override
    public JsonResult update(ApplyCustoms applyCustoms, User loginUser) {
        if (applyCustomsBiz.update(applyCustoms, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(ApplyCustoms applyCustoms, User loginUser) {
        if (applyCustomsBiz.remove(applyCustoms, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
